﻿@using Partner.Surround.Web.Startup
@using Partner.Surround.Authorization

@section styles{
    <link href="~/css/treetable.css" rel="stylesheet" />
    <link href="~/lib/layui-extend-modules/ztree/metroStyle/metroStyle.css" rel="stylesheet" />
}

<!--数据字典列表-->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-btn-container">
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_Delete))
                {
                    <button class="layui-btn layuiadmin-btn-dataDictionary" data-type="batchDelDataDictionary">删除类型</button>
                }
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_Create))
                {
                    <button class="layui-btn layuiadmin-btn-dataDictionary" data-type="addDataDictionary">添加类型</button>
                }
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_Update))
                {
                    <button class="layui-btn layuiadmin-btn-dataDictionary" data-type="editDataDictionary">编辑类型</button>
                }
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_DataDictionaryItem_Delete))
                {
                    <button class="layui-btn layuiadmin-btn-dataDictionary" data-type="batchDelDataDictionaryItem">删除字典项</button>
                }
                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_DataDictionaryItem_Create))
                {
                    <button class="layui-btn layuiadmin-btn-dataDictionary" data-type="addDataDictionaryItem">添加字典项</button>
                }
            </div>

            <div class="layui-row">
                <div class="layui-col-md2 layui-col-xs12">
                    <ul id="tree" class="ztree" style="padding: 0px; border: 1px solid #ddd; overflow: auto;"></ul>
                </div>
                <div class="layui-col-md10 layui-col-xs12">
                    <table class="layui-table"
                           lay-data="{height: 'full-100', page:false, id:'dataDictionaryItemList'}"
                           lay-filter="dataDictionaryItemList">
                        <thead>
                            <tr>
                                <th lay-data="{checkbox:true, fixed: true}"></th>
                                <th lay-data="{field:'code'}">业务代码</th>
                                <th lay-data="{field:'name'}">名称</th>
                                @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_DataDictionaryItem_Update) ||
                             await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_DataDictionaryItem_Delete))
                                {
                                    <th lay-data="{fixed:'right', align:'center', toolbar: '#dataDictionaryItemBarList'}"></th>
                                }
                            </tr>
                        </thead>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<!--列表按钮-->
<script type="text/html" id="dataDictionaryItemBarList">
    <div class="layui-table-cell laytable-cell-1-0-9">
        @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_DataDictionaryItem_Update))
        {
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        }
        @if (await PermissionChecker.IsGrantedAsync(PermissionNames.Pages_Base_DataDictionary_DataDictionaryItem_Delete))
        {
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        }
    </div>
</script>

@section scripts{
    <script type="text/javascript">
        layui.config({
            base: '/lib/layui-extend-modules/' //静态资源所在路径
        }).extend({
            ztree: 'ztree/ztree'
        }).use(['jquery', 'form', 'ztree', 'table'], function () {
            var $ = layui.$,
                form = layui.form,
                ztree = layui.ztree,
                table = layui.table;

            //主列表加载，可反复调用进行刷新
            var config = {};  //table的参数，如搜索key
            var loadDataDictionaryItemList = function (options) {
                if (options != undefined) {
                    $.extend(config, options);
                }
                table.reload('dataDictionaryItemList', {
                    url: "@Url.Action("GetDataDictionaryItemList", "DataDictionary",new { area="Base" })",
                    where: config,
                    parseData: function (res) {
                        return {
                            "code": res.result.code, //解析接口状态
                            "msg": res.result.msg, //解析提示文本
                            "count": res.result.count, //解析数据长度
                            "data": res.result.data //解析数据列表
                        };
                    },
                    response: {
                        statusCode: 200 //规定成功的状态码，默认：0
                    }
                });
            };

            //左边树状样品类型列表
            var zTree = function () {
                var zTreeObj;
                var setting = {
                    view: { selectedMulti: false },
                    data: {
                        key: {
                            name: 'typeName',
                            title: 'typeName'
                        },
                        simpleData: {
                            enable: true,
                            idKey: 'id',
                            rootPId: 'null'
                        }
                    },
                    callback: {
                        onClick: function (event, treeId, treeNode) {
                            loadDataDictionaryItemList({ "dataDictionaryId": treeNode.id });
                        }
                    }
                };
                var load = function () {
                    abp.ajax({
                        type:"Get",
                        url: "@Url.Action("GetDataDictionaryList", "DataDictionary", new { area= "Base" })"
                    }).done(function (treeNodes) {
                        zTreeObj = $.fn.zTree.init($("#tree"), setting);
                        zTreeObj.addNodes(null, treeNodes.data);
                        zTreeObj.expandAll(true);
                    });
                };
                load();
                return {
                    reload: load
                };
            }();
            $("#tree").height($("div.layui-row").height() - 2);

            //常用弹框新增或更新展示
            function createOrUpdateLayer(layerData) {
                parent.layer.open({
                    type: 2,
                    title: layerData.title,
                    content: layerData.content,
                    area: layerData.area,
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].layui.submitForm();
                    }
                });
            }

            //常用弹框删除展示
            function deleteLayer(deleteData) {
                parent.layer.confirm('确定删除吗?', function (index) {
                    abp.ajax({
                        url: deleteData.targetUrl,
                        data: JSON.stringify(deleteData.data)
                    }).done(function (data) {
                        deleteData.callback(data);
                    });

                    parent.layer.close(index);
                });
            };

            //删除字典类型
            function deleteDataDictionary(data) {
                var deleteData = {
                    targetUrl:"@Url.Action("DeleteDataDictionary", "DataDictionary", new { area = "Base" })",
                    data: data,
                    callback: function (returnData) {
                        if (returnData.code == 200) {
                            abp.notify.success(returnData.msg);
                            zTree.reload();//刷新树
                        }
                    }
                };
                deleteLayer(deleteData);
            };

            //删除字典项
            function deleteDataDictionaryItem(data) {
                var deleteData = {
                    targetUrl:"@Url.Action("DeleteDataDictionaryItem", "DataDictionary", new { area = "Base" })",
                    data: data,
                    callback: function (returnData) {
                        if (returnData.code == 200) {
                            abp.notify.success(returnData.msg);
                            loadDataDictionaryItemList();
                        }
                    }
                };
                deleteLayer(deleteData);
            };

            //创建或更新字典类型完毕回调
            layui.callback = function (data) {
                parent.layer.close(data.index); //关闭弹层
                abp.notify.success(data.msg);
                zTree.reload();//刷新树
            }

            //创建或更新字典项完毕回调
            layui.dataDictionaryItemCallback = function (data) {
                parent.layer.close(data.index); //关闭弹层
                abp.notify.success(data.msg);
                loadDataDictionaryItemList({ "dataDictionaryId": data.dataDictionaryId});
            }

            //监听工具条
            table.on('tool(list)', function (obj) {
                var data = obj.data;
                if (obj.event === 'del') {   //删除
                    deleteDataDictionaryItem([{"id": data.id}]);
                } else if (obj.event === 'edit') {  //编辑
                    var dataDictionaryItemData = {
                        title: '编辑字典项信息',
                        content: "@Url.Action("CreateOrUpdateDataDictionaryItem", "DataDictionary", new { area = "Base" })" + abp.utils.formatString("?dataDictionaryItemId={0}", data.id),
                        area: ["440px", "364px"]
                    };
                    createOrUpdateLayer(dataDictionaryItemData);
                }
            });

            //监听页面主按钮操作
            var active = {
                batchDelDataDictionary: function () {
                    var treeObj = $.fn.zTree.getZTreeObj("tree");
                    var currSelectedNode = treeObj.getSelectedNodes();
                    if (currSelectedNode == undefined || currSelectedNode.length == 0|| currSelectedNode[0].id==null) {
                        parent.layer.msg("请先选择字典类型");
                        return;
                    }

                    deleteDataDictionary([{ "id": currSelectedNode[0].id }]);
                },
                batchDelDataDictionaryItem: function () {
                    var checkStatus = table.checkStatus('dataDictionaryItemList'),
                        checkData = checkStatus.data; //得到选中的数据

                    if (checkData.length === 0) {
                        return parent.layer.msg('请选择数据');
                    }

                    var ids = checkData.map(function (d) { return { "id": d.id }; });
                    deleteDataDictionaryItem(ids);
                },
                addDataDictionary: function () {//添加字典类型
                    var dataDictionaryData = {
                        title: '添加字典类型',
                        content: "@Url.Action("CreateOrUpdateDataDictionary", "DataDictionary", new { area = "Base" })",
                        area: ["440px", "364px"]
                    };
                    createOrUpdateLayer(dataDictionaryData);
                },
                editDataDictionary: function () {//更新字典类型
                    var treeObj = $.fn.zTree.getZTreeObj("tree");
                    var currSelectedNode = treeObj.getSelectedNodes();
                    if (currSelectedNode == undefined || currSelectedNode.length == 0|| currSelectedNode[0].id == null) {
                        parent.layer.msg("请先选择字典类型");
                        return;
                    }

                    var dataDictionaryData = {
                        title: '编辑字典类型',
                        content: "@Url.Action("CreateOrUpdateDataDictionary", "DataDictionary", new { area = "Base" })" + abp.utils.formatString("?dataDictionaryId={0}", currSelectedNode[0].id),
                        area: ["440px", "364px"]
                    };
                    createOrUpdateLayer(dataDictionaryData);
                },
                addDataDictionaryItem: function () {//添加字典项
                    var treeObj = $.fn.zTree.getZTreeObj("tree");
                    var currSelectedNode = treeObj.getSelectedNodes();
                    if (currSelectedNode == undefined || currSelectedNode.length == 0|| currSelectedNode[0].id == null) {
                        parent.layer.msg("请先选择字典类型");
                        return;
                    }

                    var dataDictionaryItemData = {
                        title: '添加字典项',
                        content: "@Url.Action("CreateOrUpdateDataDictionaryItem", "DataDictionary", new { area = "Base" })" + abp.utils.formatString("?dataDictionaryId={0}", currSelectedNode[0].id),
                        area: ["440px", "364px"]
                    };
                    createOrUpdateLayer(dataDictionaryItemData);
                },
            };

            //点击事件转发器
            $('.layui-btn.layuiadmin-btn-dataDictionary').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
}

